package com.gmlive.common.sample.http

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity
import com.gmlive.common.okhttplog.IkOkHttpLogInterceptor
import com.gmlive.common.sample.R
import kotlinx.android.synthetic.main.activity_http_log.*
import okhttp3.*
import java.io.IOException

class HttpLogActivity : AppCompatActivity() {
    var test1 = "https://service.boluohuyu.cn/api/all/app_rank_top4"
    var test2 = "https://service.xiangshengclub.com/api/user/info?source_info=eyJhcHBpZCI6IjYwMDAwIiwidWlkIjoiODUyNzM5IiwicGFnZSI6ImNvbS5tZWVsaXZlLmluZ2tl%0AZS5idXNpbmVzcy5tYWluLnVpLk1haW5BY3Rpdml0eSIsInRpbWUiOiIxNTY2MjAwNzcwNzA2In0%3D%0A&cc=TG49203&code1018679893=1566200782162&cpu=&lc=36e55ccad9483903&devi=957969fdf791f692f68d16b5a1b709a3&osversion=android_25&ndid=&imei=867306031697546&conn=4G&xid=87&ram=0&icc=89860085191475153233&ast=1&imsi=460000762613167&mtid=0e6928c226ebeb964759787ed1e64f6e&mtxid=dc9914f7d692&cv=EP2.0.00_Android&proto=8&logid=1002&ua=XiaomiMI5X&vv=1.0.3-201610121413.android&aid=d474ef250c90919a&with_album=true"
    var test3 = "https://service.xiangshengclub.com/log/upload?cv=EP2.0.00_Android&lc=36e55ccad9483903&uid=852739&cc=TG49203&devi=957969fdf791f692f68d16b5a1b709a3&smid=DuHEqfXArjJCjJzZWCi9L7rvQbeSAPqsOf%2Bqhh8V%2BSp1ZYmmoAJSKHhaKSCgdr%2FOVti27KiY563iWedTzsRJaCSg&source_info=eyJhcHBpZCI6IjYwMDAwIiwidWlkIjoiODUyNzM5IiwiYXBwVG9rZW4iOiI5RjFHOVJTREhSMjJI%0AMlI1SDVXRDlISFdSRzI4RzQxNCIsInVzZXJUb2tlbiI6Imc4dzQ2OXJmMjU5c2g0czJ3ZGc5OHdk%0AbmQxNnNyOHIxIiwicGFnZSI6ImNvbS5tZWVsaXZlLmluZ2tlZS5idXNpbmVzcy51c2VyLmFjY291%0AbnQudWkuT3RoZXJVc2VySG9tZUFjdGl2aXR5IiwidGltZSI6IjE1NjYyMDA3ODIxNzQifQ%3D%3D%0A&aid=d474ef250c90919a&ua=XiaomiMI5X&imsi=460000762613167&imei=867306031697546&icc=89860085191475153233&conn=wifi&osversion=android_25&mtid=ec4a3f0b8ee7f7da684651917016b109&mtxid=7844fd592208&seq=16"
    var test4 = "https://service.xiangshengclub.com/api/activity/banner_list?source_info=eyJhcHBpZCI6IjYwMDAwIiwidWlkIjoiODUyNzM5IiwicGFnZSI6ImNvbS5tZWVsaXZlLmluZ2tl%0AZS5idXNpbmVzcy5tYWluLnVpLk1haW5BY3Rpdml0eSIsInRpbWUiOiIxNTY2MjY3Nzg1MTM3In0%3D%0A&cc=TG49203&cpu=%5BAdreno_%28TM%29_506%5D%5BAArch64_638_Qualcomm_Technologies%2C_Inc_MSM8953%5D&lc=36e55ccad9483903&devi=b20c07a17410b8db09c96ded3b7f4f82&osversion=android_25&sid=20spcrVlzRCxm8Zc0SwzMQcJBtkJZIKi2H5bLRFn3NmTDpKeeci3&ndid=&imei=867306031697546&conn=wifi&xid=50&ram=3772047360&icc=89860085191475153233&ast=1&imsi=460000762613167&mtid=0e6928c226ebeb964759787ed1e64f6e&mtxid=dc9914f7d692&cv=EP2.0.00_Android&proto=8&logid=&ua=XiaomiMI5X&uid=852739&vv=1.0.3-201610121413.android&smid=DuHEqfXArjJCjJzZWCi9L7rvQbeSAPqsOf%2Bqhh8V%2BSp1ZYmmoAJSKHhaKSCgdr%2FOVti27KiY563iWedTzsRJaCSg&aid=d474ef250c90919a&code621187361=1566267785153&type=1"

    var mCall: Call? = null
    private val loggerBuilder = StringBuilder()
    private val logger = IkOkHttpLogInterceptor.Logger { message: String? ->
        runOnUiThread {
            loggerBuilder.append(message)
            loggerBuilder.append('\n')
            http_logging.text = loggerBuilder.toString()
            logger_container.fullScroll(ScrollView.FOCUS_DOWN)
        }
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.do_request1 -> url_edit.setText(test1)
            R.id.do_request2 -> url_edit.setText(test2)
            R.id.do_request3 -> url_edit.setText(test3)
            R.id.do_request4 -> url_edit.setText(test4)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_http_log)

        do_request1.setOnClickListener {
            onClick(it)
        }

        do_request2.setOnClickListener {
            onClick(it)
        }

        do_request3.setOnClickListener {
            onClick(it)
        }

        do_request4.setOnClickListener {
            onClick(it)
        }

        do_request.setOnClickListener { doRequest() }
    }

    override fun onDestroy() {
        if (mCall != null) {
            mCall!!.cancel()
        }
        super.onDestroy()
    }

    private fun doRequest() {
        loggerBuilder.setLength(0)
        val url = url_edit.text.toString()
        val httpLogger = IkOkHttpLogInterceptor(logger)
        httpLogger.level = IkOkHttpLogInterceptor.Level.BODY
        val client = OkHttpClient.Builder()
                .addInterceptor(httpLogger)
                .build()
        val request = Request.Builder()
                .url(url)
                .get() //默认就是GET请求，可以不写
                .build()
        mCall = client.newCall(request)
        mCall!!.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.d(TAG, "onFailure: " + e.message)
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                Log.d(TAG, "onResponse: " + response.body!!.string())
            }
        })
    }

    companion object {
        private const val TAG = "HttpLogger"
    }
}